﻿using HomagChina.Basic.CCT.Db.CCHS_Db;
using HomagGroup.Base.UI;
using HomagGroup.Base.UI.Controls;
using HomagGroup.Base.UI.Windows;
using Proxy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using static HomagChina.CCHCS.Utility;

namespace HomagChina.CCHCS.ViewModels
{
    public class ManualInfeedViewModel : NotifyPropertyChangedBase
    {
        #region Handle

        private SearchInfoHandle Handle_Search;
        private InsertInfoHandle Handle_Insert;
        private ModifyInfoHandle Handle_modify;
        private CommonHandle commonHandle;

        #endregion Handle

        #region 属性

        public RelayCommand ApplyCommand { get; }

        private int pcs;
        private string description;

        public int Pcs
        {
            get => pcs;
            set { pcs = value; OnPropertyChanged(); }
        }

        public string Description
        {
            get => description;
            set { description = value; OnPropertyChanged(); }
        }

        public DropDownList DropDownList_InfeedEntry { get; set; }
        public DropDownList DropDownList_TargetPostion { get; set; }
        public DropDownList DropDownList_BoardCode { get; set; }
        public DropDownList DropDownList_DummyBoard { get; set; }

        #endregion 属性

        public ManualInfeedViewModel()
        {
            Handle_Search = new SearchInfoHandle();
            Handle_Insert = new InsertInfoHandle();
            Handle_modify = new ModifyInfoHandle();
            commonHandle = new CommonHandle();
            ApplyCommand = new RelayCommand(Apply);
            //LoadDropDownList();
        }

        private void LoadDropDownList()
        {
            //获取所有的库位信息
            var positionInfos = Handle_Search.GetPositionInfos();
            DropDownList_InfeedEntry = new DropDownList
            {
                ItemsSource = positionInfos.Where(s => s.Positiontype.Equals(0)).Select(s => s.Positioncode).ToList()
            };
            DropDownList_InfeedEntry.SelectedItem = DropDownList_InfeedEntry.Items.CurrentItem;
            DropDownList_InfeedEntry.SelectedIndex = 0;

            //获取所有的物料信息
            var materialList = Handle_Search.GetMaterialList();
            DropDownList_BoardCode = new DropDownList
            {
                ItemsSource = materialList
            };
            //DropDownList_BoardCode.SelectedItem = DropDownList_BoardCode.Items.CurrentItem;
            //DropDownList_BoardCode.SelectedIndex = 0;

            DropDownList_DummyBoard = new DropDownList
            {
                ItemsSource = (new List<string>() { { GetLocalText("Not Need") } })//{ GetLocaText("Need") },
            };
            DropDownList_DummyBoard.SelectedItem = DropDownList_DummyBoard.Items.CurrentItem;
            DropDownList_DummyBoard.SelectedIndex = 0;
            //获取可用货位信息
            var Availablelocations = Handle_Search.GetAvailablelocations();
            var lis = new List<string>();
            //lis.Add(GetLocalText("System allocated"));
            foreach (var item in Availablelocations.ToArray())
            {
                lis.Add(item);
            }
            DropDownList_TargetPostion = new DropDownList
            {
                ItemsSource = lis
            };
            DropDownList_TargetPostion.SelectionChanged += DropDownList_TargetPostion_SelectionChanged;
            DropDownList_TargetPostion.SelectedItem = DropDownList_TargetPostion.Items.CurrentItem;
            DropDownList_TargetPostion.SelectedIndex = 0;
        }

        private void DropDownList_TargetPostion_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            T_CCHS_Stock stockInfo = Handle_Search.GetStockInfos().FirstOrDefault(s => s.Positioncode.Equals(DropDownList_TargetPostion.SelectedItem));
            if (stockInfo != null)
            {
                string material = stockInfo.Material;
                DropDownList_BoardCode.SelectedItem = material == "" ? null : material;
            }
        }

        public void Apply()
        {
            DateTime dateTime = DateTime.Now;
            var InfeedPosition = DropDownList_InfeedEntry.SelectedValue.ToString();
            var TargetPosition = DropDownList_TargetPostion.SelectedValue.ToString();
            var DummyBoard = DropDownList_DummyBoard.SelectedValue.ToString();

            if (DropDownList_BoardCode.SelectedValue == null)
            {
                MessageBox.Show("Please select the material", "Warning");
            }
            else
            {
                var Material = DropDownList_BoardCode.SelectedValue.ToString();
                Returnstructure re = Handle_Insert.ManuallyInfeed(Material, InfeedPosition, TargetPosition, pcs, DummyBoard == GetLocalText("Need"));
                HomagGroup.Base.UI.Windows.MessageBox.Show(re.Msg.ToString());
                commonHandle.CloseDiaLog(GetLocalText("Manual Infeed"));
            }
        }
    }
}